package com.briup.jd2511.web03.util;

import lombok.Data;

import java.io.Serial;
import java.io.Serializable;

/**
 * 统一结果响应：以同一个类型的结果作为响应返回
 */
@Data
public class Result implements Serializable {
    @Serial
    private static final long serialVersionUID = 1L;
    private Integer code;
    private String msg;
    private Object data;
    private Long timestamp;

    public Result(ResultCode resultCode, Object data){
        this.code = resultCode.getCode();
        this.msg = resultCode.getMsg();
        this.data = data;
        this.timestamp = System.currentTimeMillis();
    }
    // 以静态方法提供一些公用的返回方法
    // 请求成功，携带数据
    public static Result ok(Object data){
        return new Result(ResultCode.SUCCESS,data);
    }
    // 请求成功，不带数据
    public static Result ok(){
        return ok(null);
    }
    // 请求失败，不带数据
    public static Result error(ResultCode resultCode){
        return new Result(resultCode,"操作失败");
    }

}
